<template>
  <div class="page-box">
    <!-- 筛选区域 -->
    <div v-show="flag" class="search-box">
      <!-- :inline="true" 表单在一行显示 -->
      <el-form size="small" :inline="true" class="demo-form-inline">
        <el-form-item label="账号">
          <el-input v-model="params.login_name" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="手机">
          <el-input v-model="params.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="params.status" placeholder="请选择">
            <el-option label="冻结" value="0" />
            <el-option label="启用" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="serach">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 工具条区域 -->
    <div class="tabbar-box">
      <el-row type="flex" justify="space-between">
        <div>
          <el-button plain type="primary" icon="el-icon-plus" size="mini" @click="addvip('add')">新增</el-button>
          <el-button plain type="warning" icon="el-icon-download" size="mini" @click="exportExcel">导出</el-button>
        </div>
        <div>
          <el-tooltip class="item" effect="dark" content="关闭搜索区" placement="top">
            <el-button :icon="up" size="small" circle @click="hide" />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="刷新" placement="top">
            <el-button icon="el-icon-refresh" size="small" circle @click="reload" />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="筛选" placement="top-start">
            <el-popover placement="bottom" width="100" trigger="click">
              <el-checkbox-group v-model="checkList">
                <el-checkbox v-for="(item,index) in renderList" :key="index" class="fa fa fa-dedent" :label="item.name" />
              </el-checkbox-group>
              <el-button slot="reference" class="filter" icon="el-icon-menu" size="small" circle />
            </el-popover>
          </el-tooltip>
        </div>
      </el-row>
    </div>
    <!-- 表格区域 -->
    <div class="table-box">
      <el-table id="outTable" ref="myTable" :data="userlist" style="width: 100%" border>
        <el-table-column type="index" label="#" width="50" />
        <el-table-column v-if="checkList.includes('账号')" prop="login_name" label="账号" />
        <el-table-column v-if="checkList.includes('姓名')" prop="name" label="姓名" />
        <el-table-column v-if="checkList.includes('邮箱')" prop="email" label="邮箱" />
        <el-table-column v-if="checkList.includes('手机号')" prop="phone" label="手机号" />
        <el-table-column v-if="checkList.includes('角色')" prop="" label="角色">
          <template #default="{row}">
            <div v-if="row.rolename.length" class="status">{{ row.rolename[0] }}</div>
          </template>
        </el-table-column>
        <el-table-column v-if="checkList.includes('创建时间')" prop="create_date" label="创建时间" />
        <el-table-column label="状态">
          <template #default="{row}">
            <el-switch
              v-model="row.status"
              style="display: block"
              active-text="启动"
              inactive-text="冻结"
              :active-value="1"
              :inactive-value="0"
            />
          </template>
        </el-table-column>
        <el-table-column prop="" label="操作">
          <template #default="{row}">
            <el-button size="mini" circle type="primary" icon="el-icon-edit" @click="addvip(row.id)" />
            <el-button size="mini" type="danger" icon="el-icon-delete" circle @click="dodelete(row.id)" />
          </template>

        </el-table-column></el-table>
    </div>
    <!-- 分页区域 -->
    <div class="page">
      <el-pagination
        v-if="isTableAlive"
        background
        :current-page="params.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="params.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
<script>
import { getuser } from '@/api/power'
import { exportTableAsXLSX } from '@/utils/unitools'
export default {
  data() {
    return {
      dialogVisible: false,
      fileList: [],
      dialog: '添加广告图',
      currentid: '',
      checkList: ['账号', '姓名', '邮箱', '手机号', '角色', '创建时间', '状态'],
      renderList: [{ name: '账号' }, { name: '姓名' }, { name: '邮箱' }, { name: '手机号' }, { name: '角色' }, { name: '创建时间' }, { name: '状态' }],
      userlist: [],
      params: {
        login_name: '',
        status: '',
        phone: '',
        page: 1,
        limit: 10
      },
      flag: true,
      total: 0,
      up: 'el-icon-arrow-up',
      // 分页显隐
      isTableAlive: true
    }
  },
  computed: {
    headers() {
      return {
        'X-Token': this.$store.getters.token
      }
    }
  },
  async created() {
    this.getuserlist()
  },
  methods: {
    exportExcel() {
      exportTableAsXLSX('outTable')
    },

    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.params.page = 1
      this.params.limit = val
      this.getuserlist()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.getuserlist()
    },
    async getuserlist() {
      const res = await getuser(this.params)
      console.log(res)
      this.userlist = res.data
      this.total = res.count
    },

    hide() {
      this.flag = !this.flag
      this.up = this.flag ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
    },
    reload() {
      this.userlist = []
      this.params = {
        sex: '',
        username: '',
        mobile: '',
        page: 1,
        limit: 10
      }
      this.isTableAlive = false
      setTimeout(() => {
        this.isTableAlive = true
      }, 100)
      this.getuserlist()
    },
    serach() {
      this.getuserlist()
    }

  }
}
</script>
<style lang="scss" scoped>
.page-box {
  padding: 20px;
  .search-box,
  .tabbar-box {
    padding: 10px 10px;
    height: 50px;
    .filter {
      margin-left: 10px;
    }
  }
  .fontsome {
    margin-left: 15px;
    &:hover {
      border-bottom: 1px solid #51a7ff;
      padding-bottom: 5px;
      color: #51a7ff;
    }
  }
}
.date {
  width: 250px;
}
.status {
  border: 1px solid #d9ecff;
  color: #67c23a;
  background-color:#f0f9eb;
  text-align: center;
  border-radius: 5px;
  width: 60px;
  height: 30px;
  font-size: 12px;
  line-height: 30px;
}
.el-pagination {
  text-align: right;
}
.green {
  background-color: #f0f9eb;
  color: #67c23a;
}
.sexswitch {
  margin-top: 7px;
}
</style>
